/*
Blender Game Engine Launcher
Copyright (C) 2011  Michael Goerlich and Matthias Esau

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "settings.h"

Settings::Settings() : good(false), doc("BGESettings")
{

}

const QRegExp Settings::resolution = QRegExp("^([0-9]{1,4})x([0-9]{1,4})$");

Settings::Settings(QString path) : good(false), doc("BGESettings")
{
    QFile file(path);

    if(file.open(QFile::ReadOnly))
    {
        if(doc.setContent(&file))
        {
            QDomElement root = doc.documentElement();

            QDomNode resolutions    = root.namedItem("resolutions");
            QDomNode paths          = root.namedItem("paths");
            QDomNode uisettings     = root.namedItem("uisettings");
            QDomNode selected       = root.namedItem("selected");

            if(!resolutions.isNull() && !paths.isNull() && !uisettings.isNull() && !selected.isNull())
            {
                good  = true;
                good &= readResolutions(resolutions);
                good &= readPaths(paths);
                good &= readUISettings(uisettings);
                good &= readSelected(selected);
            }
        }
    }
    file.close();
}

bool Settings::readResolutions(QDomNode& node)
{
    QDomNode res;
    QDomNode text;
    for(unsigned int i=0; i<node.childNodes().length(); i++)
    {
        res = node.childNodes().at(i);

        if(getTextNode(res, "resolution", text))
        {
            resolutions.append(text.nodeValue());
        }
        else if(getTextNode(res, "customresolution", text))
        {
            customResolution = text;
        }
        else
        {
            return false;
        }
    }

    if(customResolution.isNull()) {
        customResolution = doc.createElement("customresolution");
        customResolution.appendChild(doc.createTextNode(""));
        node.appendChild(customResolution);
    }

    return true;
}

bool Settings::readPaths(QDomNode& node)
{
    QDomNode child;
    QDomNode text;

    if(node.childNodes().length()==4)
    {
        for(unsigned int i=0; i<node.childNodes().length(); i++)
        {
            child = node.childNodes().at(i);
            if(getTextNode(child, "icon", text))
            {
                iconPath = text.nodeValue();
            }
            else if(getTextNode(child, "logo", text))
            {
                logoPath = text.nodeValue();
            }
            else if(getTextNode(child, "blenderplayer", text))
            {
                playerPath = text.nodeValue();
            }
            else if(getTextNode(child, "blendfile", text))
            {
                blendfilePath = text.nodeValue();
            }
        }
    }

    return (!iconPath.isEmpty() && !logoPath.isEmpty() && !playerPath.isEmpty() && !blendfilePath.isEmpty());
}

bool Settings::readUISettings(QDomNode& node)
{
    QDomNode child;
    QDomNode text;

    if(node.childNodes().length()==1)
    {
        for(unsigned int i=0; i<node.childNodes().length(); i++)
        {
            child = node.childNodes().at(i);
            if(getTextNode(child, "windowtitle", text))
            {
                windowTitle = text.nodeValue();
            }
        }
    }

    return !windowTitle.isNull();
}

bool Settings::readSelected(QDomNode& node)
{
    QDomNode child;
    QDomNode text;

    if(node.childNodes().length()==3)
    {
        for(unsigned int i=0; i<node.childNodes().length(); i++)
        {
            child = node.childNodes().at(i);
            if(getTextNode(child, "resolution", text))
            {
                selectedResolution = text;
            }
            else if(getTextNode(child, "antialiasing", text))
            {
                selectedAntialiasing = text;
            }
            else if(getTextNode(child, "fullscreen", text))
            {
                selectedFullscreen = text;
            }
        }
    }

    return (!selectedResolution.isNull() && !selectedAntialiasing.isNull() && !selectedFullscreen.isNull());
}

bool Settings::getTextNode(QDomNode &node, QString proposedName, QDomNode &textNode)
{
    if(node.nodeName()==proposedName && node.childNodes().length()==1)
    {
        textNode = node.childNodes().at(0);
        return true;
    }
    return false;
}

bool Settings::writeTo(QString path)
{
    bool ret = false;
    QFile settingsfile(path);

    if(settingsfile.open(QIODevice::WriteOnly)) {
        QTextStream ts(&settingsfile);
        settingsfile.write(doc.toString().toAscii().data());
        ret = true;
        ts.flush();
    }
    settingsfile.close();

    return ret;
}
